package com.loan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loan.entity.QuickApply;
import com.loan.exeception.AppException;
import com.loan.exeception.AppExceptionCodeMsg;
import com.loan.mapper.QuickApplyMapper;
import com.loan.service.QuickApplyService;
import com.loan.service.SendSmsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
* @author z
* @description 针对表【quick_apply】的数据库操作Service实现
* @createDate 2023-04-21 14:51:41
*/
@Service
public class QuickApplyServiceImpl extends ServiceImpl<QuickApplyMapper, QuickApply> implements QuickApplyService {

    @Resource
    private QuickApplyMapper quickApplyMapper;
    @Resource
    private SendSmsService sendSmsService;
    @Override
    public void addQuickApply(QuickApply quickApply) {
        //获取申请贷款金额
        BigDecimal amount = quickApply.getAmount();
        //判断金额是否合规>10W
        if (amount.intValue()<10){
            throw new AppException(AppExceptionCodeMsg.AMOUNT_NOT_ILLEGAL);
        }else {
            //判断是否输入手机号码
            String phone = quickApply.getPhone();
            if (phone==null || "".equals(phone)){
                throw new AppException(AppExceptionCodeMsg.PHONE_NUMBER_IS_NULL);
            }else {
                //将验证码进行比对
                boolean checkSms = sendSmsService.checkSms(quickApply.getPhone(),quickApply.getCode());
                if (checkSms){
                    //如果返回true，进行数据库插入操作
                    quickApplyMapper.insert(quickApply);
                }else {
                    throw new AppException(AppExceptionCodeMsg.CODE_NUMBER_IS_ERROR);
                }
            }
        }

    }
}




